package com.wk.springboot.thread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.Iterator;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool {

    public static void main(String[] args) {

        //创建ThreadFactoryBuilder
        ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
        threadFactoryBuilder.setNameFormat("thread-running-%s");
        //创建守护线程
        threadFactoryBuilder.setDaemon(false);
        threadFactoryBuilder.setUncaughtExceptionHandler(new MyUncaughtExceptionHandle());

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,5,3000,
                TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(3), threadFactoryBuilder.build()
                ,new ThreadPoolExecutor.DiscardPolicy());

        try{
            Runnable[] runnables = new Runnable[10];
            for(int i=0;i<10;i++){
                runnables[i] = new MyThread("thread-"+i);
            }
            for(int i=0;i<10;i++){
                threadPoolExecutor.execute(runnables[i]);
            }

            System.out.println("主线程已经执行完成！！！");
        }finally {
            threadPoolExecutor.shutdown();
        }
    }
}
